<?php

class Router {

    public function run() {
        $this->_cmd();
        $this->_html();
    }

    public function _cmd() {
        if (!isset($_SERVER['argv']) || !$_SERVER['argv'] || !is_array($_SERVER['argv'])) {
            return;
        }

        if ($_SERVER['argv'][0] != 'index.php') {
            return;
        }

        $classname = isset($_SERVER['argv'][1]) ? trim($_SERVER['argv'][1]) : '';
        $method = isset($_SERVER['argv'][2]) ? trim($_SERVER['argv'][2]) : '';
        if (!$classname) {
            return;
        }

        $method || $method = 'execute';
        _autoload('controller/' . $classname);
        $controller = new $classname;
        if (!method_exists($controller, $method)) {
            throw new \Exception(sprintf('对象[%s]不存在方法[%s]', $classname, $method));
        }

        call_user_func([$controller, $method]);
    }

    public function _html() {
        if (!isset($_SERVER['REQUEST_FLAG']) || $_SERVER['REQUEST_FLAG'] <= 0) {
            return;
        }
        $defaultController = 'UserLoginIn';
        $defaultMethod = 'execute';
        getInstance()->response = $_SERVER['HTTP_RESPONSE'];
        $paths = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
        $pathNum = count($paths);
        if ($pathNum == 0 || !trim($paths[0])) {
            $this->isCallback($defaultController, $defaultMethod);
            return;
        }
        if (in_array($paths[0], ['index.php', 'index.html', 'index'], TRUE)) {
            if (!$paths) {
                $this->isCallback($defaultController, $defaultMethod);
                return;
            }
            if (count($paths) == 3) {
                $controller = $this->parseController($paths[1]);
                $method = $this->parseMethod($paths[2]);
                $this->isCallback($controller, $method);
                return;
            }
            $this->responseEroor(__LINE__);
            return;
        }
        if ($pathNum == 2) {
            $controller = $this->parseController($paths[0]);
            $method = $this->parseMethod($paths[1]);
            $this->isCallback($controller, $method);
            return;
        }
        if ($pathNum == 1 && trim($paths[0]) == 'favicon.ico') {
            return;
        }
        $this->responseEroor(__LINE__);
    }

    private function isCallback($controller, $method) {
        if (!$controller || !$method) {
            $this->responseEroor(__LINE__);
            return FALSE;
        }
        try {
            _autoload('controller/' . $controller);
            if (!class_exists($controller)) {
                $this->responseEroor(__LINE__);
                return FALSE;
            }
            $object = new $controller;
            if (method_exists($object, $method)) {
                call_user_func([$object, $method]);
                return TRUE;
            }
        } catch (\Exception $e) {
        }
        $this->responseEroor(__LINE__);
        return FALSE;
    }

    private function parseController($controller) {
        return implode(array_map('ucfirst', explode('_', strtolower($controller))));
    }

    private function parseMethod($method) {
        return substr($method, 0, strpos($method, '.html'));
    }

    private function responseEroor($line) {
        getInstance()->response->end(sprintf('Bad Request[%d]', $line));
    }
}
